var add_movies = new Array();

// send the search request to imdb
function search_imdb(search_term)
{
	if(search_term != '') {
		document.getElementById('loading_search').style.display = 'inline';
		// clear previous search terms
		document.getElementById('search_results').innerHTML = '';
		document.getElementById('search_btn').disabled = true;
		document.getElementById('search_contain').style.display = 'block';
		// process the search
		ajax_http_request('index.php?what=movie&do=search_results&title=' + search_term, 'process_search');
	}
}

// get the results back from imdb and display them
function process_search(data)
{
	sr = document.getElementById('search_results');
	if(data != '') {
		// we have search results
		movies = data.split('\n');
		// loop through movies and display them
		for(i = 0; i < movies.length; i++) {
			movie = movies[i].split('|');
			li = document.createElement('li');
			li.setAttribute('onclick', 'store_movie(\''+ movie[0] +'\', \''+ movie[1].replace('\'', '\\\'') +' ('+ movie[2] +')\');');
			li.setAttribute('id', movie[0]);
			li.innerHTML =  movie[1] +' ('+ movie[2] +')';
			sr.appendChild(li);
		}
	} else {
		sr.innerHTML = 'No results could be found for <strong>'+ document.getElementById('title').value +'</strong>';
	}
	// update the styles on the search
	_clean_search();

	document.getElementById('loading_search').style.display = '';
	document.getElementById('search_btn').disabled = false;
}

function store_movie(val, title)
{
	document.getElementById('search_select_pane').style.display = 'block';
	var pos = _movie_in_array(val, true);
	if(pos === false) {
		add_movies[add_movies.length] = new Array(val, title);
		obj = document.getElementById(val);
		if(obj) {
			obj.className = 'selected';
		}
	} else {
		// since the movie is already in the array, lets remove it!
		tmp = new Array();
		var j = 0;
		for(var i = 0; i < add_movies.length; i++) {
			if(pos != i) {
				tmp[j] = add_movies[i];
				j++;
			}
		}
		add_movies = tmp;
		_clean_search();
	}
	_build_select_list();
}

function remove_movie(id)
{

	if(id == '*') {
		add_movies = new Array();
	} else {
		tmp = new Array();

		for(i = 0; i < add_movies.length; i++) {
			if(add_movies[i][0] != id) tmp[tmp.length] = add_movies[i];
		}
		add_movies = tmp;
	}
	_clean_search();
	_build_select_list();
}


function do_submit()
{
	for(i = 0; i < add_movies.length; i++) {
		add_field('frm_imdb_results','hidden', 'id['+add_movies[i][0]+']', add_movies[i][1]);
	}
}

function _build_select_list()
{
	sl = document.getElementById('search_selected');
	// truncate the liast
	sl.innerHTML = '';

	for(i = 0; i < add_movies.length; i++) {
		var li = document.createElement('li');
		li.setAttribute('onclick', 'remove_movie(\''+ add_movies[i][0] +'\');');
		li.innerHTML = add_movies[i][1];
		if(i % 2 == 0) {
			li.setAttribute('class', 'even');
		} else {
			li.setAttribute('class', 'odd');
		}
		sl.appendChild(li);
		//sl.innerHTML += '<div><strong onclick="remove_movie(\''+ add_movies[i][0] +'\')">X</strong> '+add_movies[i][1] +'</div>';
	}

}

function _movie_in_array(imdb, pos)
{
	pos = pos || false;
	if(add_movies.length > 0) {
		for(var i = 0; i < add_movies.length; i++) {
			if(add_movies[i][0] == imdb) {
				// title exists
				if(pos) {
					return i;
				}
				return true;
			}
		}
	}
	return false;
}

// run through the search and only select the movies in the select list
function _clean_search()
{
	srs = document.getElementById('search_results').getElementsByTagName('li');
	// loop through search results
	for(var i = 0; i < srs.length; i++) {
		imdb = srs[i].id;
		if(_movie_in_array(imdb)) {
			srs[i].setAttribute('class', 'selected');
		} else if(i % 2 == 0) {
			srs[i].setAttribute('class', 'even');
		} else {
			srs[i].setAttribute('class', 'odd');
		}
	}
}